package com.hbhmzy.call;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.view.View.OnClickListener;
import android.widget.Toast;

import com.hbhmzy.utils.Client;
import com.hbhmzy.utils.ConfigEntity;

import static com.hbhmzy.call.WantCallFragment.symbol;

public class MainActivity extends FragmentActivity implements OnClickListener {
    private ImageButton tab1;
    private ImageButton tab2;
    private ImageButton tab3;
    private ViewPager viewPager;
    public List<Fragment> fList;
    public static String name;

    LocalReceiver_start localReceiver_start;
    LocalReceiver_off localReceiver_off;

    public static WeakReference<MainActivity> mainActivityWeakReference;
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mainActivityWeakReference = new WeakReference<>(this);
        initView();
        initEvent();
        rejectbroadcast();
    }

    private void initView() {
        tab1 = findViewById(R.id.main_tab_want_call_img);
        tab2 = findViewById(R.id.main_tab_address_img);
        tab3 = findViewById(R.id.main_tab_latest_call_img);

        viewPager = findViewById(R.id.id_viewpager);
        fList = new ArrayList<>();

        Fragment tab1 = new WantCallFragment();
        Fragment tab2 = new AddressFragment();
        Fragment tab3 = new LatestCallFragment();

        fList.add(tab1);
        fList.add(tab2);
        fList.add(tab3);

        FragmentPagerAdapter fpAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return fList.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return fList.get(arg0);
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }
        };
        viewPager.setAdapter(fpAdapter);
        viewPager.setOffscreenPageLimit(2);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                int currentItem = viewPager.getCurrentItem();
                setTab(currentItem);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });


        //获取本机号码，并赋值给name；
        Intent intent=getIntent();
        name=intent.getStringExtra("name");

        //登录成功后 UDP和TCP同时通知服务器 已上线
        try {
            new Client(ConfigEntity.serverIP, ConfigEntity.serverTCPPort, MainActivity.this).action();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Client.send(name + "login");
    }

    private void initEvent() {
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_tab_want_call_img:
                setSelect(0);
                break;
            case R.id.main_tab_address_img:
                setSelect(1);
                break;
            case R.id.main_tab_latest_call_img:
                setSelect(2);
                break;
            default:
                break;
        }
    }

    private void setSelect(int i) {
        // 1、切换内容区域的Fragment
        setTab(i);
        viewPager.setCurrentItem(i);
    }

    //设置图片、文字为亮色
    private void setTab(int i) {
        tab1.setImageResource(R.mipmap.tag_want_call_nor22);
        tab2.setImageResource(R.mipmap.tag_address_nor);
        tab3.setImageResource(R.mipmap.tag_latest_call_nor);
        switch (i) {
            case 0:
                tab1.setImageResource(R.mipmap.tag_want_call_sel11);
                break;
            case 1:
                tab2.setImageResource(R.mipmap.tag_address_sel);
                break;
            case 2:
                tab3.setImageResource(R.mipmap.tag_latest_call_sel);
                break;

        }
    }

    private void rejectbroadcast() {
        //注册本地广播接收器 接收"hbhmzy.start"类型广播
        Log.e("tag", "rejectbroadcast: start");
        IntentFilter intentFilter_start = new IntentFilter();
        intentFilter_start.addAction("hbhmzy.start");
        localReceiver_start = new LocalReceiver_start();
        this.getApplication().registerReceiver(localReceiver_start, intentFilter_start);


        //注册本地广播接收器 接收"hbhmzy.offline"类型广播
        Log.e("tag", "rejectbroadcast: offline");
        IntentFilter intentFilter_off = new IntentFilter();
        intentFilter_off.addAction("hbhmzy.offline");
        localReceiver_off = new LocalReceiver_off();
        this.getApplication().registerReceiver(localReceiver_off, intentFilter_off);
    }

    //当有电话打过来的时候 跳转到等待接听页面
    class LocalReceiver_start extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Intent intent1 = new Intent(MainActivity.this, WaitCallActivity.class);
            Bundle bundle = new Bundle();
            bundle.putBoolean("is_online", false);
            bundle.putString("symbol", symbol);
            intent1.putExtras(bundle);
            startActivity(intent1);
        }
    }
    //当点击电话，对方不在线时。
    class LocalReceiver_off extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(MainActivity.this, "您拨打的用户暂时不在线，请稍后再拨", Toast.LENGTH_SHORT).show();
        }
    }
}
